package com.example.blog.controller;

import com.example.blog.constant.Constants;
import com.example.blog.model.BlogInfo;
import com.example.blog.service.BlogService;
import com.example.blog.service.UserService;
import com.example.blog.utils.JwtUtils;
import io.jsonwebtoken.Jwt;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-05
 * Time: 13:10
 */
@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping(value = "/getBlogList", method = RequestMethod.GET)
    public List<BlogInfo> getBlogList() {
        log.info("获取博客列表");
        return blogService.getBlogList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("根据博客id返回博客信息，getBlogDetail,blogId:{}", blogId);
        //获取博客文章信息
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //根据token获取userId
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId != null && userId == blogDetail.getUserId()){
            blogDetail.setLoginUser(true);
        }else{
            blogDetail.setLoginUser(false);//不加也无所谓，默认为false
        }
        return blogDetail;
    }

    /**
     * 发布文章
     */
    @RequestMapping("/add")
    public Boolean addBook(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("添加图书,bloginfo:{}", blogInfo);
        //参数校验
        if (!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())) {
            return false;
        }
        //获取登录用户
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null || userId <= 0){
            return false;
        }
        blogInfo.setUserId(userId);
        return blogService.insertBook(blogInfo);
    }

    /**
     * 根据博客文章id，删除博客文章
     * @param blogId
     * @return
     */
    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("删除博客，blogId：{}",blogId);
        if(blogId <= 0){
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);//删除
        return blogService.delete(blogInfo);
    }

    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客，blogInfo：{}",blogInfo);
        //参数校验
        if(blogInfo.getId() == null
                || !StringUtils.hasLength(blogInfo.getTitle())
                || !StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        log.info("参数校验成功——id:{},title:{},content:{}",blogInfo.getId(),blogInfo.getTitle(),blogInfo.getContent());
        return blogService.update(blogInfo);
    }


}
